<?php

namespace App\Http\Controllers\Test;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Service\Ip;
use Illuminate\Support\Facades\DB;


class TestController extends Controller
{
    // 测试
    public function initialize()
    {
        parent::initialize();
    }

    // 取消html标签
    public function index()
    {
        $_rangeHtml = '<p style="padding: 3px 0;"><img style="max-width:98%;" src="https://slh.caitongnews.com/caiji/weixin/2023/202309/20/202309201695178901.5876701338070.jpg"></p><p style="padding: 3px 0;">印度的市场确实很香，但汽车作为大众消费品，对人群购买力的要求要远高于手机这类普通消费品，需要更为长期的兑现期。</p>';
        $_rangeHtml = strip_tags($_rangeHtml, '<img>');
        echo $_rangeHtml;
    }

    // 获取ip
    // slh.com/test/test/ip
    public function ip()
    {
        $ip = $this->request->ip();
        $ip = '127.0.0.1';
        $ip = '118.116.110.35';

        return (new Ip())->getIpAddress($ip);
    }

    // 查看当前时间
    public function domain()
    {
        $domain = request()->getHost();

        var_dump($domain);
        $domain = $_SERVER['HTTP_HOST'];
        var_dump($domain);

        return $this->success(['domain' => $domain]);
    }

    // 查看当前时间
    public function time()
    {
        return $this->success(['time' => LARAVEL_TIME]);
    }

    // 测试远程图片是否存在
    public function image()
    {
        $url = 'http://tp2.sinaimg.cn/1652845545/50/5679800235/1';
        $url = 'http://qzapp.qlogo.cn/qzapp/100232596/F6145BC9A088532FFAEE2CB8BE804EB1/100';
        $nr  = file_get_contents($url);

        var_dump($nr);

        return $this->success([$nr]);
    }

    // 用户查询单条记录
    public function user()
    {
        $field = $this->userField();
        $uids  = [2, 7, 8, 9, 10, 13, 15, 16, 17, 19, 20, 21, 23, 24, 25, 26, 27, 28, 29, 30];
        $uids  = [10008, 10009, 10010, 10011, 10012, 10013, 10014, 10015, 10016, 10017, 10018, 10019, 10020, 10021, 10022, 10023, 10024,];
        $lists = (new User())->isCache(true)->getListInIds($uids, $field);

        //$lists = (new User())->isCache(true)->getOneById(20, $field);

        return $this->success($lists);
    }

    // 将微信公众号转化成用户信息
    public function ddsfdfgfsdgfghdt_toUser()
    {
        $wechatDb = new SiteWechat();
        $userDb   = new User();
        $modiDb   = new UserModify();
        $veriDb   = new UserVerify();
        $time     = LARAVEL_TIME;


        $data = $wechatDb->getList([], 0);
        foreach($data as $item){
            $apply_type  = 1;
            $description = $item['signature'];
            $areas       = '[]';
            $position    = $item['title'];
            if($position == '深蓝财经'){
                continue;
            }
            // 添加 认证
            $data1 = [
                'realname'   => $position,
                'username'   => $position,
                'city'       => '成都市',
                'is_apply'   => 1,
                'apply_type' => $apply_type,  // ?1
                'verify'     => 1,
                'is_virtual' => '2',
                'position'   => $position,    // ?2
                'ctime'      => $time,
                'mtime'      => $time,
                'head_img'   => $item['avatar'],
            ];
            $uid   = $userDb->insertGetId($data1);

            // 认证
            $verify = [
                'uid'           => $uid,
                'apply_type'    => $apply_type,      // ?1
                'realname'      => $position,
                'position'      => $position,        // ?2
                'audit_content' => '系统用户',
                'areas'         => $areas,           // ?4
                'description'   => $description,     // ?3
                'files'         => '[]',
                'links'         => '[]',
                'verify'        => 1,
                'ctime'         => $time,
                'mtime'         => $time,
            ];
            $uid2   = $veriDb->insertGetId($verify);

            // 重新赋值
            $data2 = [
                'uid'         => $uid,
                'invite_code' => $uid,
                'description' => $description,     // ?3
                'city'        => '成都市',
                'position'    => $position,        // ?2
                'areas'       => $areas,           // ?4
                'files'       => '[]',
                'links'       => '[]',
                'ctime'       => $time,
                'mtime'       => $time,
            ];
            $uid3  = $modiDb->insertGetId($data2);
        }

        return $this->success('创建微信采集用户成功！');
    }

    // 生成系统用户
    public function sysuser()
    {
        // 参数
        $realname    = '中国证券网报';
        $description = '中国证券网报';

        // 组合sql
        $userDb = new User();
        $veriDb = new UserVerify();
        $modiDb = new UserModify();
        $time   = LARAVEL_TIME;
        $img    = rand(1, 75);
        $head   = '/static/sys_head_img/face_'.$img.'.jpg';
        // 判断是否已经存在
        $info = $userDb->where('realname', $realname)->first();
        if(!empty($info)){
            return $this->error('该用户已经存在！');
        }
        // 组合sql
        $apply_type = 1;
        $areas      = '[]';
        $position   = $realname.'官方账号';
        // 添加
        $data1 = [
            'realname'   => $realname,
            'username'   => $realname,
            'city'       => '成都市',
            'is_apply'   => 1,
            'apply_type' => $apply_type,  // ?1
            'verify'     => 1,
            'is_virtual' => '2',
            'position'   => $position,    // ?2
            'ctime'      => $time,
            'mtime'      => $time,
            'head_img'   => '/static/sys_head_img/face_'.$img.'.jpg',
        ];
        // 认证
        $verify = [
            'uid'           => 0,
            'apply_type'    => $apply_type,      // ?1
            'realname'      => $realname,
            'position'      => $position,        // ?2
            'audit_content' => '系统用户',
            'areas'         => $areas,           // ?4
            'description'   => $description,     // ?3
            'files'         => '[]',
            'links'         => '[]',
            'verify'        => 1,
            'ctime'         => $time,
            'mtime'         => $time,
        ];
        // 重新赋值
        $data2 = [
            'uid'         => 0,
            'invite_code' => 0,
            'description' => $description,     // ?3
            'city'        => '成都市',
            'position'    => $position,        // ?2
            'areas'       => $areas,           // ?4
            'files'       => '[]',
            'links'       => '[]',
            'ctime'       => $time,
            'mtime'       => $time,
        ];
        // 执行sql
        DB::beginTransaction();
        try{
            // 保存用户主表
            $uid = $userDb->insertGetId($data1);
            // 重新赋值
            $verify['uid']        = $uid;
            $data2['uid']         = $uid;
            $data2['invite_code'] = $uid;
            // 保存附表与审核
            $uid2 = $veriDb->insertGetId($verify);
            $uid3 = $modiDb->insertGetId($data2);

            DB::commit();
        } catch(\Exception $e){
            DB::rollBack();

            return $this->error($e->getMessage());
        }

        return $this->success([$uid]);
    }

    // 添加公共用户或者虚拟用户
    //  slh.com/test/test/sysuser3
    //  https://www.shenlanhui.com/test/test/sysuser3
    //  slh.shenlannews.com/test/test/sysuser3
    public function sysuser3()
    {
        // 参数
        // 1:虚拟用户
        // 2:系统用户
        // 3:公众号或者采集的用户
        $usertype = 2;
        $realname = '深蓝之声';
        $realname = '深蓝汇快讯';
        $realname = '深蓝招聘';
        $realname = '深蓝币';
        $model    = new User();
        $is_auth  = true; // 是否认证

        $result = $model->addSysUser($usertype, $realname, $is_auth);
        if($result === false){
            return $this->error($model->getError());
        }

        return $this->success($result);
    }


    public function sysuser4()
    {
        // 参数
        $realname    = '黑马创业';
        $description = '黑马创业';
        // 组合sql
        $ctime = LARAVEL_TIME;
        $img   = rand(1, 75);
        $head  = '/static/sys_head_img/face_'.$img.'.jpg';
        $model = new User();
        // 判断是否已经存在
        $info = $model->where('realname', $realname)->first();
        if(!empty($info)){
            return $this->error('该用户已经存在！');
        }

        // 组合sql
        $sql1 = "INSERT INTO `shl_user_verify` (`uid`, `apply_type`, `realname`, `position`, `audit_content`, `areas`, `description`, `files`, `links`, `is_look`, `verify`, `ctime`, `mtime`) VALUES
(".'$to_uid'.",	1,	'".$realname."',	'".$realname."',	'系统用户',	'[]',	'".$description."',	'[]',	'[]',	0,	1,	'".$ctime."',	'".$ctime."');";
        $sql2 = "INSERT INTO `shl_user_modify` (`uid`,`invite_code`,`city`, `position`, `files`, `links`, `areas`, `description`, `ctime`, `mtime`) VALUES
(".'$to_uid'.", ".'$to_uid'.", '成都市', '".$realname."',	'[]',	'[]',	'[]',	'".$description."',	'".$ctime."',	'".$ctime."');";
        $main = "INSERT INTO `shl_user` (`username`, `realname`, `head_img`, `position`, `is_apply`,`apply_type`, `city`, `is_virtual`, `verify`, `ctime`, `mtime`) VALUES
('".$realname."','".$realname."',	'".$head."',	'".$realname."',	1,	'1', '成都市',	2,	1,	'".$ctime."',	'".$ctime."');";

        // 执行sql
        DB::beginTransaction();
        try{
            // 保存用户主表
            $_uid = $model->insert($main);
            $sql1 = str_replace('$to_uid', $_uid, $sql1);
            $sql2 = str_replace('$to_uid', $_uid, $sql2);

            var_dump($_uid);
            // 保存附表与审核
            $model->insert($sql1);
            $model->insert($sql2);

            DB::commit();
        } catch(\Exception $e){
            DB::rollBack();

            return $this->return_false($e->getMessage());
        }


    }

    // 测试 oneAdd 添加不成功（结果：操作成功）
    public function add()
    {
        $data  = ['rank' => 1, 'title' => '测试'];
        $model = new Tag();
        $res   = $model->oneAdd($data);

        return $this->success($res);
    }

    // 获取保存的系统用户，用户采集深蓝财经的文章
    public function get()
    {
        $lists  = (new User())->where('is_virtual', 2)->select(['id', 'username'])->get()->toArray();
        $br     = '<br>';
        $space  = '&nbsp;&nbsp;&nbsp;&nbsp;';
        $string = '['.$br;
        foreach($lists as $item){
            $string .= $space.$item['id']." => ['id' => ".$item['id'].", 'username' => '".$item['username']."',],".$br;
        }
        $string .= ']'.$br;

        echo $string;
    }

    // 删除二维数组最后一个元素
    public function del()
    {
        $data = (new Admin())->getAll();
        var_dump($data);
        echo '<br/>';
        echo '<br/>';
        echo '<br/>';
        echo '<br/>';
        array_pop($data);
        var_dump($data);
        echo '<br/>';
        echo '<br/>';
        echo '<br/>';
        echo '<br/>';

    }

    // 测试
    // http://www.atw.com/test/test/test
    public function test()
    {
        // 关联数组，不能使用 ... 符号
        $data = ['in' => 'aa', 'my' => 'bb', 'cd' => 'cc'];
        var_dump($data);
        echo '<br><br>';

        $data = ['aa', 'bb', 'cc'];
        var_dump(...$data);
        echo $this->func(...$data);
        echo '<br><br>';

        // 1、添加中国 国家知识产权局      国家信息中心
        $brief = '【港交所旗下LME或将香港纳入为大宗商品交收地】据香港信报，消息人士称，地方港交所旗下伦敦金属交易所（LME）正考虑将香港纳入为大宗商品交易仓库和交收地点，以响应政府发展规划。据业界人士消息指，LME早前有与阿里巴巴物流旗舰菜鸟集团会面，探讨建设大宗商品交易生态圈的合作。 (复旦复)';
        // 2、取消来源 最后一位是 ） )
        $brief = $this->removeOrigin($brief);


        echo '<br><br>';
        var_dump($brief);
    }

    // 去除 来源
    protected function removeOrigin($brief)
    {
        $lastChar = mb_substr($brief, -1, 1, 'UTF-8');
        $lastPos  = '';
        if($lastChar == ')'){
            $lastPos = mb_strrpos($brief, '(', 0, 'UTF-8');
            if($lastPos === false){
                $lastPos = mb_strrpos($brief, '（', 0, 'UTF-8');
            }
        } elseif($lastChar == '）'){
            $lastPos = mb_strrpos($brief, '（', 0, 'UTF-8');
            if($lastPos === false){
                $lastPos = mb_strrpos($brief, '(', 0, 'UTF-8');
            }
        }
        if($lastPos){
            $brief = mb_substr($brief, 0, $lastPos, 'UTF-8');
        }

        return $brief;
    }

    // 引入组件
    public function func($a, $b, $c)
    {
        $str = '$a+2*$b+3$c = '."$a+2*$b+3$c";

        return $str;
    }

}


